﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using System.Collections;

public partial class admin_Documents : System.Web.UI.Page
{
     public ArrayList Doc = null;
     public int id = 0;
     
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            lb_error.Visible = false;
            lb_error2.Visible = false;

            Doc = (ArrayList)DB.getArrDocuments ("");
            if (Request.QueryString["id"] != null) id = int.Parse(Request.QueryString["id"].ToString());
            
            lb_Docid.Text = id.ToString();
            llenoDoc();

                RE_GenericBean Docs = DB.getDocumentsType1(id);
                foreach (RE_GenericBean rg in Doc) {
                    if (rg.intC2 == 1) {
                        rg.strC5 = "<img src='images/icons/action_check.gif' alt='' > ";
                    
                    }
                }
                    
                    DropDownList1.SelectedValue = Docs.intC4.ToString();
                    lb_Docid.Text = Docs.intC1.ToString();
                    tb_Doc.Text = Docs.strC1.Trim();
                    tb_Order.Text = Docs.intC3.ToString();
                    tb_Description.Text = Docs.strC3.ToString();
                    
                        if (Docs.intC2 == 1)
                            {
                                CBmandatory.Checked = true;
                            }
                        
                        if (Docs.intC1 != 0)
                            {
                                LinkButton4_ModalPopupExtender.Show();
                            }
                        else
                
                            {
                                id = int.Parse(lb_Docid.Text);
                            }
        }
        
        else
        {
                lb_error2.Visible = false;
                lb_error.Visible = false;
                Doc = (ArrayList)DB.getArrDocuments ("");
        }
    }

    //if (Session["user"] == null) { Response.Redirect("../Index.aspx"); }

    protected void LinkButton1_Click(object sender, EventArgs e)
    {

        if (tb_Doc.Text == "" || DropDownList1.SelectedIndex == 0)
       
        {
            LinkButton4_ModalPopupExtender.Show();
            lb_error.Visible = true;

        }
        else
        {
            RE_GenericBean Docs = new RE_GenericBean();
            bool edit = false;

                Docs.intC1 = int.Parse(lb_Docid.Text);
                Docs.strC1 = tb_Doc.Text.Trim().ToUpper();
                Docs.intC3 = int.Parse(DropDownList1.SelectedValue);
                Docs.strC3 = tb_Description.Text.Trim().ToUpper();

                    if (CBmandatory.Checked == true)
                    {
                        Docs.intC2 = 1;
                    }
                    else
                    {
                        Docs.intC2 = 0;
                    }

            if (Docs.intC1 != 0) edit = true;
            if (DB.InsertDoc(Docs, edit))
            {
                Response.Redirect("Documents.aspx");
            }
        }

    }

    private void llenoDoc()
    { 
        ArrayList doc = null;
        ListItem item = new ListItem();
        item.Value = "0";
        item.Text= "-Select one-";

        DropDownList1.Items.Add(item);
        try
        {
            doc = (ArrayList)DB.getArrTypeDoc  ("");
            if (doc != null && doc.Count > 0)
            {
                foreach (RE_GenericBean c in doc)
                {
                    item = new ListItem(c.strC1, c.intC1.ToString());
                    DropDownList1.Items.Add(item);
                }
            }
        }
        catch (Exception err)
        {
            utility.log(err.StackTrace);
        }
    }

    protected void LinkButton4_Click(object sender, EventArgs e)
    {
        LinkButton4_ModalPopupExtender.Show();

    }

    protected void LinkButton3_Click(object sender, EventArgs e)
    {
        int p;

        p = DB.getDocEmp (int.Parse(lb_Docid.Text));

        if (p == 0)
        {
            int id = int.Parse(lb_Docid.Text);
            if (id != 0)
            {
                int result = DB.DeleteDoc(id);
                Response.Redirect("Documents.aspx");
            }
        }
        else
        {
            lb_error2.Visible = true;
            LinkButton4_ModalPopupExtender.Show();

        }
        
    }
    
    protected void Button1_Click1(object sender, EventArgs e)
    {
        Response.Redirect("Documents.aspx");
    }

    protected void Button1_Click(object sender, EventArgs e)  
    {
        Response.Redirect("Documents.aspx");
    }

    protected void LinkButton5_Click(object sender, EventArgs e)
    {
        Response.Redirect("Documents.aspx");

    }

    protected void btnCuentaCancelar_Click1(object sender, EventArgs e)
    {
        Response.Redirect("Documents.aspx");
    }

    protected void CBmandatory_CheckedChanged(object sender, EventArgs e)
    {

    }
}

